package cn.kailangtu.modbus;

import android.util.Log;

import cn.kailangtu.commontools.CommonUtils;

public class ModbusCommandTcp {
    private byte[] affairsNum;
    private byte[] protocol;
    private byte[] length;
    private byte unit;
    private byte function;
    private byte[] data;
    private byte[] startStore;
    private byte[] storeCount;
    private byte byteCount;


    public static ModbusCommandTcp buildCommand(String command){
        return buildCommand(CommonUtils.hexStringToByteArray(command));
    }
    public static ModbusCommandTcp buildCommand(byte[] command){
        int length = CommonUtils.byteArrayToInt16(new byte[]{command[4],command[5]});
        if(command.length == (length+6)){
            ModbusCommandTcp modbusCommandTcp = new ModbusCommandTcp();
            modbusCommandTcp.setAffairsNum(new byte[]{command[0],command[1]});
            modbusCommandTcp.setProtocol(new byte[]{command[2],command[3]});
            modbusCommandTcp.setLength(new byte[]{command[4],command[5]});
            modbusCommandTcp.setUnit(command[6]);
            modbusCommandTcp.setFunction(command[7]);
            modbusCommandTcp.setStartStore(new byte[]{command[8],command[9]});

            if(modbusCommandTcp.getFunction() == 6)
            {
                modbusCommandTcp.setData(new byte[]{command[10], command[11]});
            }
            else {
                modbusCommandTcp.setStoreCount(new byte[]{command[10], command[11]});
                if (command.length > 12) {
                    byte[] data = new byte[command.length - 12];
                    System.arraycopy(command, 12, data, 0, data.length);
                    modbusCommandTcp.setData(data);
                }
            }



            return modbusCommandTcp;
        }
        return null;
    }




    public byte[] receive(byte[] data)
    {
        int resultLenght = 10;
        if(data != null ){
            resultLenght =data.length+resultLenght;
        }
        else{
            resultLenght +=2;
        }
        byte[] result = new byte[resultLenght];
        System.arraycopy(affairsNum,0,result,0,2);
        System.arraycopy(protocol,0,result,2,2);
        byte[] length = CommonUtils.int2ByteArray(result.length - 6);
        System.arraycopy(length,0,result,4,2);

        result[6] = unit;
        result[7] = function;

        if(function == 0x03 || function == 0x04) {
            //byte[] dataLength =  CommonUtils.int2ByteArray (data.length);
            result[8] = (byte)data.length;
            System.arraycopy(data, 0, result, 9, data.length);
        }
        else if(function == 0x06)
        {
            System.arraycopy(startStore,0,result,8,2);
            System.arraycopy(data,0,result,10,data.length);
        }
        else if(function == 0x10){
            System.arraycopy(startStore,0,result,8,2);
            System.arraycopy(storeCount,0,result,10,2);
        }


        return result;
    }


    public byte[] getAffairsNum() {
        return affairsNum;
    }

    public void setAffairsNum(byte[] affairsNum) {
        this.affairsNum = affairsNum;
    }

    public byte[] getProtocol() {
        return protocol;
    }

    public void setProtocol(byte[] protocol) {
        this.protocol = protocol;
    }

    public byte[] getLength() {
        return length;
    }

    public void setLength(byte[] length) {
        this.length = length;
    }

    public byte getUnit() {
        return unit;
    }

    public void setUnit(byte unit) {
        this.unit = unit;
    }

    public byte getFunction() {
        return function;
    }

    public void setFunction(byte function) {
        this.function = function;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public byte[] getStartStore() {
        return startStore;
    }

    public void setStartStore(byte[] startStore) {
        this.startStore = startStore;
    }

    public byte[] getStoreCount() {
        return storeCount;
    }

    public void setStoreCount(byte[] storeCount) {
        this.storeCount = storeCount;
    }

    public byte getByteCount() {
        return byteCount;
    }

    public void setByteCount(byte byteCount) {
        this.byteCount = byteCount;
    }
}
